import { createRouter, createWebHashHistory } from 'vue-router'

import Layout from '@/layout/index.vue'
// import Layout from './main'

export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    hidden: true
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/error/403.vue'),
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    hidden: true
  },
  {
    path: '/msgQueue1',
    redirect: '/msgQueue',
    component: Layout,
    children: [
      {
        path: '/msgQueue',
        name: 'msgQueue',
        component: () => import('@/views/msgQueue/index.vue'),
        meta: {
          // title: 'msgQueue',
          // icon: 'msgQueue'
        },
        hidden: true
      }
    ]
  },
  // 司机订单详情
  {
    path: '/driverOrder',
    name: 'driverOrder',
    hidden: true,
    component: () =>
      import('@/views/fleet/carBooking/component/DriverOrderDialog.vue')
  }
]

// 所有需要动态控制的路由定义，会在权限模块根据后台返回的权限信息过滤
export const asyncRoutes = [
  // 车队管理
  {
    path: '/',
    name: 'fleet',
    component: Layout,
    redirect: '/fleet/vehicle',
    alwaysShow: true,
    children: [
      {
        path: '/fleet/vehicle',
        name: 'vehicle',
        component: () => import('@/views/fleet/vehicle/index.vue'),
        meta: {
          title: 'vehicleManage'
        }
      },
      {
        path: '/fleet/carBooking',
        name: 'carBooking',
        component: () => import('@/views/fleet/carBooking/index.vue'),
        meta: {
          title: 'carBookingManage'
        }
      },
      {
        path: '/fleet/historyOrder',
        name: 'historyOrder',
        component: () => import('@/views/fleet/carBooking/index.vue'),
        meta: {
          title: '历史订单'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: 'fleetManage',
      index: '1',
      icon: 'fleet'
    }
  },
  // 酒店管理
  {
    path: '/hotel',
    name: 'hotel',
    component: Layout,
    redirect: '/hotel/room',
    alwaysShow: false,
    children: [
      {
        path: '/hotel/room',
        name: 'room',
        component: () => import('@/views/hotel/room/index.vue'),
        meta: {
          title: 'roomManage'
        }
      },
      {
        path: '/hotel/booking',
        name: 'roomBooking',
        component: () => import('@/views/hotel/booking/index.vue'),
        meta: {
          title: 'roomBookingManage'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: 'hotelManagement',
      index: '1',
      icon: 'hotel'
    }
  },
  // 票务管理
  {
    path: '/ticket',
    name: 'ticket',
    component: Layout,
    redirect: '/ticket/booking',
    alwaysShow: true,
    children: [
      // {
      //   path: '/ticket/entrance',
      //   name: 'entrance',
      //   component: () => import('@/views/ticket/entrance/index.vue'),
      //   meta: {
      //     title: 'entranceTicketManage'
      //   }
      // },
      {
        path: '/ticket/booking',
        name: 'booking',
        component: () => import('@/views/ticket/booking/index.vue'),
        meta: {
          title: 'bookingManage'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: 'ticketManage',
      index: '1',
      icon: 'ticket'
    }
  },
  // 旅行社管理
  {
    path: '/travelAgency',
    name: 'travelAgency',
    component: Layout,
    redirect: '/travelAgency/category',
    alwaysShow: true,
    children: [
      {
        path: '/travelAgency/category',
        name: 'category',
        component: () => import('@/views/travelAgency/category/index.vue'),
        meta: {
          title: '类别管理'
        }
      },
      {
        path: '/travelAgency/route',
        name: 'route',
        component: () => import('@/views/travelAgency/route/index.vue'),
        meta: {
          title: 'routeManage'
        }
      },
      {
        path: '/travelAgency/tourDate',
        name: 'tourDate',
        component: () => import('@/views/travelAgency/tourDate/index.vue'),
        meta: {
          title: 'tourDateManage'
        }
      },
      {
        path: '/travelAgency/costCalc',
        name: 'costCalc',
        component: () => import('@/views/travelAgency/costCalc/index.vue'),
        meta: {
          title: 'costCalculator'
        }
      },
      {
        path: '/travelAgency/quote',
        name: 'quote',
        component: () => import('@/views/travelAgency/quote/index.vue'),
        hidden: true
      },
      {
        path: '/travelAgency/quote/detail',
        name: 'quoteDetail',
        component: () => import('@/views/travelAgency/quote/detail.vue'),
        hidden: true
      },
      {
        path: '/travelAgency/supplier',
        name: 'supplier',
        component: () => import('@/views/travelAgency/supplier/index.vue'),
        meta: {
          title: 'supplier'
        }
      },
    ],
    meta: {
      keepAlive: false,
      title: 'travelAgencyManage',
      index: '1',
      icon: 'travelAgency'
    }
  },
  {
    path: '/messageManage',
    name: 'systemPush',
    component: Layout,
    alwaysShow: true,
    redirect: '/system/push',
    children: [
      {
        path: '/system/push',
        name: 'systemPushIndex',
        component: () => import('@/views/message/systemPush/index.vue'),
        meta: {
          title: '系统推送'
        }
      },
      {
        path: '/ai/helper',
        name: 'aiHelper',
        component: () => import('@/views/message/aiHelper/index.vue'),
        meta: {
          title: '智能助手'
        }
      },
      {
        path: '/chat/message',
        name: 'chatMessage',
        component: () => import('@/views/message/chatMessage/index.vue'),
        meta: {
          title: '聊天消息'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: '消息管理',
      index: '3',
      icon: 'push'
    }
  },
  // 用户中心
  {
    path: '/userCenter',
    name: 'userCenter',
    component: Layout,
    redirect: '/userCenter/department',
    alwaysShow: true,
    children: [
      // {
      //   path: '/userCenter/organize',
      //   name: 'organize',
      //   component: () => import('@/views/userCenter/organize/index.vue'),
      //   hidden: true,
      //   meta: {
      //     title: 'organizeManage'
      //   }
      // },
      {
        path: '/userCenter/department',
        name: 'department',
        component: () => import('@/views/userCenter/department/index.vue'),
        meta: {
          title: 'deptManage'
        }
      },
      {
        path: '/userCenter/user',
        name: 'user',
        component: () => import('@/views/userCenter/user/index.vue'),
        meta: {
          title: 'userManage'
        }
      },
      {
        path: '/userCenter/role',
        name: 'role',
        component: () => import('@/views/userCenter/role/index.vue'),
        meta: {
          title: 'roleManage'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: 'userCenter',
      index: '3',
      icon: 'userCenter'
    }
  },
  // 应用管理
  {
    path: '/apply',
    name: 'apply',
    component: Layout,
    redirect: '/apply/platform',
    alwaysShow: true,
    children: [
      {
        path: '/apply/platform',
        name: 'platformApply',
        component: () => import('@/views/apply/platform/index.vue'),
        meta: {
          title: 'platformApply'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: 'applyManage',
      index: '2',
      icon: 'apply'
    }
  },
  {
    path: '/customer',
    name: 'customer',
    component: Layout,
    alwaysShow: false,
    redirect: '/customer/manage',
    children: [
      {
        path: '/customer/manage',
        name: 'customerManage',
        component: () => import('@/views/customer/index.vue'),
        meta: {
          title: '客户管理'
        }
      }
    ],
    meta: {
      keepAlive: false,
      title: '客户管理',
      index: '3',
      icon: 'userCenter'
    }
  }
]

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 })
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  g_router && g_router.clearRoutes()
  // const newRouter = createRouter({
  //   history: createWebHashHistory(),
  //   routes: constantRoutes,
  //   // 刷新时，滚动条位置还原
  //   scrollBehavior: () => ({ left: 0, top: 0 })
  // })
  // router.matcher = newRouter.matcher // reset router
  constantRoutes.forEach(item => {
    g_router && g_router.addRoute(item)
  })
}

// 点击菜单时，捕捉报错，刷新页面
router.onError(error => {
  if (error.message.includes('Failed to fetch dynamically imported module')) {
    ElMessage({
      message: g_t('route.routerUpgradeTips'),
      type: 'warning',
      duration: 1500,
      offset: 60
    })
    // reload() 类似于点击浏览器上的刷新页面按钮
    location.reload()
  }
})

export default router
